import { Controller } from "../../mvc/core/Controller";
import { ISpeakerController } from "./interfaces/ISpeakerController";
import { IView } from "../../mvc/interfaces/IView";
import { SpeakerModule } from "../../../modules/SpeakerModule";
import { EventName } from "../mvc/EventName";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { ISpeakerModule } from "../../../modules/interfaces/ISpeakerModule";
import { GameManager } from "../manager/GameManager";
import { ModuleType } from "../view/common/ModuleType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { EChatType } from "../../../Message/Public/EChatType";
import { GameProxy } from "../mvc/GameProxy";
import { SChatMsg } from "../../../Message/Game/SChatMsg";
import { FilterText } from "../../../com/gengine/utils/FilterText";
import { MsgTypeImpl } from "../manager/MsgTypeImpl";
import { PlayerMiniInfo } from "../model/PlayerMiniInfo";
//class SpeakerController
    
    export  class SpeakerController extends Controller implements ISpeakerController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._speakerModule == null) 
            {
                this._speakerModule = new SpeakerModule();
                this._speakerModule.addEventListener(EventName.ChatShowSpeaker, this.onShowSpeakerHandler);
            }
            return this._speakerModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.ChatShowSpeaker, this.onShowSpeakerHandler);
            Dispatcher.addEventListener(EventName.ChatSpeakerSend, this.onSendSpeakerMsgHandler);
            NetDispatcher.addCmdListener(ServerCommand.ChatTrumpetMessageUpdate, this.onChatTrumpetMessageUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            Dispatcher.addEventListener(EventName.ChatResize, this.onChatResizeHandler);
            NetDispatcher.addCmdListener(ServerCommand.VIPInfoUpdateCommand, this.onVipInfoUpdateHandler);
            return;
        }

        /* internal  */onShowSpeakerHandler(arg1: DataEvent): void
        {
            (view as ISpeakerModule).resetSpeakTxt();
            GameManager.instance.popupWindow(ModuleType.Speaker);
            if (!view.isHide) 
            {
                (view as ISpeakerModule).updateSpeakerNum();
                (view as ISpeakerModule).checkSpreakNum();
            }
            return;
        }

        /* internal  */onSendSpeakerMsgHandler(arg1: DataEvent): void
        {
            if (cache.role.entityInfo.level < 30) 
            {
                MsgManager.showRollTipsMsg(Language.getString(35415));
                return;
            }
            var loc1=EChatType._EChatTypeTrumpet;
            var loc2=arg1.data as string;
            var loc3=(view as SpeakerModule).textColor;
            GameProxy.chatProxy.SendMessage(loc1, loc2, loc3);
            return;
        }

        /* internal  */onChatTrumpetMessageUpdateHandler(arg1: Object): void
        {
            var loc8=null;
            var loc1=arg1 as SChatMsg;
            var loc2=loc1.chatType;
            if (loc2 != EChatType._EChatTypeTrumpet) 
            {
                return;
            }
            var loc3=loc1.fromPlayer.name;
            var loc4=loc1.content;
            loc4 = "：" + FilterText.instance.getFilterStr(loc4);
            var loc5=new Array<any>(16763904, 268435200, 65280, 255, 16711935);
            var loc6;
            var loc7;
            if ((loc7 = (loc6 = new Array<any>(16763904, 268435200, 65280, 255, 16711935)).indexOf(loc1.font)) == -1) 
            {
                loc8 = "0xFF0000";
            }
            else 
            {
                loc8 = loc5[loc7];
            }
            var loc9=new MsgTypeImpl(null, loc8, 15000);
            var loc10=new PlayerMiniInfo(loc1.fromPlayer);
            if (loc1.gmFlag) 
            {
                loc10.isGM = true;
            }
            if (loc1.guideFlag) 
            {
                loc10.isGuide = true;
            }
            if (cache.role.playerInfo.name == loc3) 
            {
                MsgManager.showRollTipsMsg(Language.getString(35400));
            }
            MsgManager.addSpeakerText(loc10, loc4, loc9);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object): void
        {
            if (!(this._speakerModule == null) && !this._speakerModule.isHide) 
            {
                this._speakerModule.updateSpeakerNum();
            }
            return;
        }

        /* internal  */onChatResizeHandler(arg1: DataEvent): void
        {
            MsgManager.chatResize();
            return;
        }

        /* internal  */onVipInfoUpdateHandler(arg1: Object): void
        {
            if (!(this._speakerModule == null) && !this._speakerModule.isHide) 
            {
                this._speakerModule.updateSpeakerNum();
            }
            return;
        }

        private /* var */_speakerModule: ISpeakerModule;
    }
